package com.fair.concurrency;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/12/27 21:48
 */
public class UseAtomicReference
{
    static AtomicReference<UserInfo> userRef = new AtomicReference<UserInfo>();

    public static void main(String[] args)
    {
        UserInfo user = new UserInfo("sowhat", 14);//要修改的实体的实例
        userRef.set(user);
        UserInfo updateUser = new UserInfo("liu", 12);//要变化的新实例
        userRef.compareAndSet(user, updateUser);
        System.out.println(userRef.get().getName());
        System.out.println(userRef.get().getAge());

        System.out.println(user.getName()); // 注意此时的user 属性
        System.out.println(user.getAge());
    }
}
